package fr.jade.service.fm.deployment;

import org.objectweb.fractal.api.Component;
import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import fr.jade.base.JadeBaseObject;
import fr.jade.reflex.api.control.LifeCycleNotificationController;
import fr.jade.reflex.util.Reflex;

public class StopTask extends JadeBaseObject implements DeploymentTask {

  protected NewComponentTask cmpTask;
  
  public StopTask(NewComponentTask cmpTask){
    assert cmpTask != null;
    this.cmpTask=cmpTask;
  }
  
  public void execute() throws DeploymentException {
    log("execute :" +toString());
    Component cmp = cmpTask.getComponent();
    try {
      LifeCycleNotificationController lcc = Reflex.getLifeCycleNotificationController(cmp);
      LifeCycleNotificationController lccDual = Reflex.getLifeCycleNotificationController(Reflex.getDualComponent(cmp));
      lcc.rawStopFcNotification();
      lccDual.rawStopFcNotification();
    } catch (NoSuchInterfaceException e) {
      throw new DeploymentException("Stop Task Error: no lifecyclecontroller", e);
    } catch (IllegalLifeCycleException e) {
      throw new DeploymentException("Stop Task Error: can't stop component", e);
    }
  }
  
  public String getLevelName(){
    return "stop";
  }
  
  public String toString(){
    return "StopTask["+ cmpTask.toString() +"]";
  }

}
